'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview pvp引导
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var soldierAttr = require('./soldierAttr');
var buildAttr = require('./buildAttr');
var buildType = require('../../consts/buildType');
var buildConf = require('../../config/buildConf');
var copyService = require('../../services/copyService');
var copyConf = require('../../config/copyConf');
var heroConf = require('../../config/heroConf');
var constantUtil = require('../../util/constantUtil');
var buildTypeUtil = require('../../util/buildTypeUtil');
var techService = require('../../services/techService');
var dataApi = require('../../libs/dataApi');
var buildService = require('../../services/buildService.js');

/**
 * 获取新手副本数据
 * @param  {[type]} pvp_guide_id [description]
 * @return {[type]}         [description]
 */
exports.getData = function (pvp_guide_id) {
    return {
        name: _getName(pvp_guide_id),
        total_exp: _getTotalExp(pvp_guide_id),
        total_gold: _getTotalGold(pvp_guide_id),
        road_list: _getRoadList(pvp_guide_id),
        build_list: _getBuildList(pvp_guide_id),
        soldier_list: _getSoldierList(pvp_guide_id),
        hero: _getHeroAttr(pvp_guide_id),
    };
};

/**
 * 获取新手副本士兵列表
 * @param  {[type]} pvp_guide_id [description]
 * @return {[type]}         [description]
 */
exports.getSoldierTypeList = function (pvp_guide_id) {
    var pvp_guide_conf = _getPvpGuideWarConf(pvp_guide_id);

    var build_list = [];
    _.each(pvp_guide_conf, function (guide_conf) {
        var build_type = buildTypeUtil.getBuildType(guide_conf.building_id);
        if (build_type == buildType.WALL) {
            build_list = _getBuildSoldierList(guide_conf.soldier_info)[0].single_soldier_list;
        }
    });
    return build_list;
};

/**
 * 获取名字
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
var _getName = function (pvp_guide_id) {
    var pvp_guide_conf = _getPvpGuideHeroConf(pvp_guide_id);
    return pvp_guide_conf.name;
};

/**
 * 获取总的经验
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
var _getTotalExp = function (copy_id) {
    return 0;
};

/**
 * 获取总的金币
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
var _getTotalGold = function (copy_id) {
    return 0;
};

/**
 * 获取道路
 * @param  {[type]} pvp_guide_id [description]
 * @return {[type]}         [description]
 */
var _getRoadList = function (pvp_guide_id) {
    var pvp_guide_conf = _getPvpGuideWarConf(pvp_guide_id);
    var road_list = [];
    _.each(pvp_guide_conf, function (guide_conf) {
        if (guide_conf.building_id == buildType.ROAD) {
            road_list.push(guide_conf.coordinate);
        }
    });
    return road_list;
};

/**
 * 获取建筑列表
 * @param  {[type]} pvp_guide_id [description]
 * @return {[type]}         [description]
 */
var _getBuildList = function (pvp_guide_id) {
    var pvp_guide_conf = _getPvpGuideWarConf(pvp_guide_id);

    var lost_proportion_total = 0;
    _.each(pvp_guide_conf, function (guide_conf) {
        // 如果是路的话路过
        if (guide_conf.building_id == buildType.ROAD) {
            return;
        }

        var build_type = buildTypeUtil.getBuildType(guide_conf.building_id);
        var building_type_conf = buildConf.getBuildTypeConf(build_type);
        if (!!building_type_conf) {
            lost_proportion_total += building_type_conf.lost_proportion;
        }
    });

    var build_list = [];
    _.each(pvp_guide_conf, function (guide_conf) {
        if (guide_conf.building_id != buildType.ROAD) {
            var build_type = buildTypeUtil.getBuildType(guide_conf.building_id);
            var building_type_conf = buildConf.getBuildTypeConf(build_type);

            // 建筑的金币比例
            var build_per = 0;
            if (!!building_type_conf && !!lost_proportion_total) {
                build_per = building_type_conf.lost_proportion / lost_proportion_total * 100;
            }
            build_list.push(_getBuild(guide_conf, build_per));
        }
    });
    return build_list;
};

/**
 * 获取士兵列表
 * @param  {[type]} copy_id       [description]
 * @return {[type]}               [description]
 */
var _getSoldierList = function (pvp_guide_id) {
    var pvp_guide_conf = _getPvpGuideWarConf(pvp_guide_id);

    var soldier_list = [];
    var soldier_type_lv_list = {};
    _.each(pvp_guide_conf, function (build_conf) {
        var soldier_info = build_conf.soldier_info;
        if (soldier_info.length <= 0) {
            return;
        }

        var soldier_conf_list = soldier_info.split(',');
        _.each(soldier_conf_list, function (soldier_conf) {
            var soldier_type = parseInt(soldier_conf.split(':')[0]);
            var soldier_lv = parseInt(soldier_conf.split(':')[1]);
            var soldier_star = parseInt(soldier_conf.split(':')[2]);
            soldier_type_lv_list[soldier_type] = {
                soldier_lv: soldier_lv,
                soldier_star: soldier_star
            };
        });
    });

    _.each(soldier_type_lv_list, function (soldier_lv_conf, soldier_type) {
        soldier_list.push(
            soldierAttr.getAttr(
                soldier_type,
                soldier_lv_conf.soldier_lv,
                soldier_lv_conf.soldier_star
            )
        );
    });
    return soldier_list;
};

/** 
 * 英雄属性
 * @param  {[type]} role_model [description]
 * @param  {[type]} bag_model  [description]
 * @return {[type]}            [description]
 */
var _getHeroAttr = function (pvp_guide_id) {
    var pvp_guide_hero_conf = _getPvpGuideHeroConf(pvp_guide_id);
    if (!pvp_guide_hero_conf) {
        return {};
    }

    var hero_type_conf = heroConf.getHeroTypeConf(pvp_guide_hero_conf.hero_type);

    // 英雄的原始属性
    var hero_attr = {
        // 最小攻击力
        min_atk: pvp_guide_hero_conf.atk || 0,

        // 最大攻击力
        max_atk: pvp_guide_hero_conf.atk || 0,

        // 火属性伤害
        fire_damage: 0,

        // 冰属性伤害
        ice_damage: 0,

        // 电属性伤害
        light_damage: 0,

        // 攻城属性伤害
        city_damage: 0,

        // 物理防御
        physical_def: 0,

        // 火属性防御
        fire_def: 0,

        // 冰属性伤害
        ice_def: 0,

        // 电属性伤害
        light_def: 0,

        // 血量
        hp: pvp_guide_hero_conf.hp || 0,

        // 命中率
        hit: 100,

        // 闪避率
        dodge: 0,

        // 爆击率
        crit: 0,

        // 反爆击率
        uncrit: 0,

        // 爆击增加值
        crit_addition: constantUtil.value('crit_addition'),

        // 攻击间隔
        atk_interval: hero_type_conf.atk_interval || 0,

        // 攻击范围
        atk_range: hero_type_conf.atk_range || 0,

        // 警界范围
        warn_range: hero_type_conf.warn_range || 0,

        // 速度
        speed: hero_type_conf.speed || 0,

        // 弱点
        weakness_list: [],

        // 抗性
        resistance_list: [],

        // 伤害加成
        soldier_damage_bonus_list: []
    };

    return {
        // 英雄职业
        hero_type: pvp_guide_hero_conf.hero_type,

        // 击杀回复血量
        kill_recover_hp: 0,

        // 击中回复血量
        hit_recover_hp: 0,

        // 每秒回复血量
        recover_hp_every_sec: 0,

        // 属性
        attr: hero_attr
    };
};

/**
 * 获取建筑的item
 * @param  {[type]} build_conf [description]
 * @return {[type]}            [description]
 */
var _getBuild = function (build_conf, build_per, build_ratio) {
    var build_id = build_conf.building_id;
    var build_lv = build_conf.building_lvl;
    var build_cell = build_conf.coordinate;
    var build_star = build_conf.building_star;
    var build_type = buildTypeUtil.getBuildType(build_id);

    var build_item = {
        build_id: build_id,
        cell: build_cell,
        per: build_per,
        star: build_star,
        attr: buildAttr.getAttr(build_type, build_lv, build_ratio)
    };

    // 附加星级加成
    buildService.getBuildStarEff(build_item);

    // var tech_type_list = ['tech_type1', 'tech_type2', 'tech_type3', 'tech_type4'];
    // var tech_value_list = ['tech_value1', 'tech_value2', 'tech_value3', 'tech_value4'];
    // for (var i = 0; i < tech_type_list.length; i++) {
    //     var tech_type = build_conf[tech_type_list[i]];
    //     var tech_value = build_conf[tech_value_list[i]];
    //     if (!!tech_type && !!tech_value) {
    //         techService.addBuildTechEff(build_item, tech_type, tech_value);
    //     }
    // }

    if (build_type == buildType.WALL) {
        build_item.soldier_list = _getBuildSoldierList(build_conf.soldier_info);
    }

    return build_item;
};

/**
 * 获取城墙的兵种数据
 * @return {Array} [description]
 */
var _getBuildSoldierList = function (wall_soldier_info) {
    var soldier_list = [];
    if (wall_soldier_info.length <= 0) {
        return soldier_list;
    }

    var batch_info = [];
    var soldier_info_list = wall_soldier_info.split(',');
    _.each(soldier_info_list, function (soldier_info_item) {
        var soldier_type = parseInt(soldier_info_item.split(':')[0]);
        batch_info.push(soldier_type);
    });
    soldier_list.push({
        single_soldier_list: batch_info
    });
    return soldier_list;
};

var _getPvpGuideHeroConf = function (pvp_guide_id) {
    return dataApi.pvp_guide_hero.findById(pvp_guide_id);
};

var _getPvpGuideWarConf = function (pvp_guide_id) {
    return dataApi.pvp_guide_war.findById(pvp_guide_id);
};
